package com.heima.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.dto.User;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.enums.MediaEnum;
import com.heima.common.minio.service.MinIOService;
import com.heima.common.util.UserThreadLocalUtil;
import com.heima.media.dto.WmMaterialDto;
import com.heima.media.entity.WmMaterial;
import com.heima.media.entity.WmNewsMaterial;
import com.heima.media.mapper.WmMaterialMapper;
import com.heima.media.service.IWmMaterialService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.media.service.IWmNewsMaterialService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * <p>
 * 自媒体图文素材信息表 服务实现类
 * </p>
 *
 * @author itheima
 * @since 2022-07-29
 */
@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements IWmMaterialService {

    @Autowired
    private MinIOService minIOService;
    @Autowired
    private IWmNewsMaterialService wmNewsMaterialService;

    /**
     * 保存自媒体用户素材
     *
     * @param file
     * @return
     */
    @Override
    public ResponseResult uploadPicture(MultipartFile file) {
        User user = UserThreadLocalUtil.get();
        if (user == null || user.getUserId() == null) {
            return ResponseResult.errorResult(401, "请登录后操作！");
        }
        //1.将文件上传到MinIO存储服务
        String upload = minIOService.upload(file);

        //2.将素材保存到素材表中
        //2.1 构建素材对象
        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setUserId(user.getUserId());
        wmMaterial.setUrl(upload);
        wmMaterial.setType(MediaEnum.MEDIA_MATERIAL_TYPE_IMG.getVal());
        wmMaterial.setIsCollection(false);
        //2.2 执行保存
        this.save(wmMaterial);
        return ResponseResult.okResult(wmMaterial);
    }

    /**
     * 分页查询自媒体用户素材列表
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult listByCollection(WmMaterialDto dto) {
        User user = UserThreadLocalUtil.get();
        if (user == null || user.getUserId() == null) {
            return ResponseResult.errorResult(401, "请登录后操作！");
        }
        //1.构建分页对象
        IPage<WmMaterial> page = new Page<>(dto.getPage(), dto.getSize());

        //2.构建查询条件
        LambdaQueryWrapper<WmMaterial> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmMaterial::getUserId, user.getUserId());
        if (ObjectUtils.isNotEmpty(dto.getIsCollection())) {
            queryWrapper.eq(WmMaterial::getIsCollection, dto.getIsCollection());
        }
        queryWrapper.orderByDesc(WmMaterial::getCreatedTime);

        //3.执行查询
        this.page(page, queryWrapper);

        //4.封装响应结果
        return new PageResponseResult(dto.getPage(), dto.getSize(), page.getTotal(), page.getRecords());
    }

    /**
     * 素材收藏
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult collrct(Integer id) {
        LambdaUpdateWrapper<WmMaterial> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(WmMaterial::getId, id);
        wrapper.set(WmMaterial::getIsCollection, true);
        boolean update = this.update(wrapper);
        if (update) {
            return ResponseResult.okResult();
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }

    /**
     * 素材取消收藏
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult cancelCollect(Integer id) {
        LambdaUpdateWrapper<WmMaterial> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(WmMaterial::getId, id);
        wrapper.set(WmMaterial::getIsCollection, false);
        boolean update = this.update(wrapper);
        if (update) {
            return ResponseResult.okResult();
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }

    /**
     * 素材删除
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteById(Integer id) {
//        判断素材是否被文章引用
        LambdaQueryWrapper<WmNewsMaterial> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WmNewsMaterial::getMaterialId, id);
        Map<String, Object> map = wmNewsMaterialService.getMap(wrapper);
        if (map!= null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_CITE);
        }else {
            boolean remove = this.removeById(id);
            if (remove) {
                return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
            }
            else {
                return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
            }
        }
    }
}
